Page({
  data: {
   order_no: ''
  },
  onLoad() {
    let that = this;
    let options = my.getLaunchOptionsSync();
    //保存订单号
    that.setData(that.data.order_no = options.query.order_no);
   // that.setData(that.data.order_no = options.order_no);
    my.getAuthCode({
      scopes: 'auth_base',
      success: (res) => {
        if(res.authCode){  
            my.request({
            url: 'https://openpay.duolaibei.com/api/callbackal',
            data:{
              'code' : res.authCode,
              'order_no' : that.data.order_no ,
            },
            headers: {
                'Content-type': 'application/json'
            },
            method: 'POST',
            success: (result) => {
              if(result.data.code == 200){
                return new Promise((resolve, reject) => {
                    my.tradePay({
                      tradeNO: result.data.content.tradeNO,
                      success: (result) => {
                        if (result.resultCode != 9000) {
                          my.confirm({
                            content: '支付异常~',
                            confirmButtonText: '确认',
                            success: (result) => {
                              my.navigateTo({
                                url:'/pages/web/web?status=3&order_no='+that.data.order_no
                              });
                            }
                          });
                        } else {
                          my.confirm({
                            content: '支付成功',
                            confirmButtonText: '确认',
                            success: (result) => {
                              my.navigateTo({
                                url:'/pages/web/web?status=1&order_no='+that.data.order_no
                              });
                            }
                          });
                        }
                      },
                      fail: (err) => {
                        my.confirm({
                          content: '支付异常！',
                          confirmButtonText: '确认',
                          success: (result) => {
                            my.navigateTo({
                              url:'/pages/web/web?status=3&order_no='+that.data.order_no
                            });
                          }
                        });
                      }
                    });
                });
              }else{
                  my.confirm({
                    content: '支付异常！',
                    confirmButtonText: '确认',
                    success: (result) => {
                      my.navigateTo({
                        url:'/pages/web/web?status=4&order_no='+that.data.order_no
                      });
                    }
                  });
              }
            },
            fail: (e) => {
              my.confirm({
                content: e,
                confirmButtonText: '确认',
                success: (result) => {
                  my.navigateTo({
                    url:'/pages/web/web?status=4&order_no='+that.data.order_no
                  });
                }
              });
            },
          });
        }else{
          my.confirm({
            content:'授权失败！请联系商家',
            confirmButtonText: '确认',
            success: (result) => {
              my.navigateTo({
                url:'/pages/web/web?status=4&order_no='+that.data.order_no
              });
            }
          });  
        }
      },
    })
  },

});
